package com.hz.house.common;

import com.hz.house.bean.entity.ArticleDetailBean;
import org.springframework.util.StringUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URL;

import java.util.List;

/**
 * 绘图工具类
 *
 * @author hsk
 */
public class ZbDraw {

    /**
     * 背景
     *
     * @param bill2d
     * @param x
     * @param y
     * @param url
     * @throws Exception
     */
    public static void drawBackGroundImg(Graphics2D bill2d, int x, int y, String url) throws Exception {
        BufferedImage bgImg = ImageIO.read(new URL(url));
        bill2d.drawImage(bgImg, 0, 0, x, y, null);
    }

    /**
     * 绘制二维码
     *
     * @param bill2d
     * @param qrCode
     * @throws Exception
     */
    public static void drawQrCode(Graphics2D bill2d, byte[] qrCode) throws Exception {
        //创建二维码的指定大小画布
        BufferedImage qrCodeImg = new BufferedImage(192, 181, BufferedImage.TYPE_INT_RGB);
        Graphics2D qr2d = qrCodeImg.createGraphics();
        ByteArrayInputStream bin = new ByteArrayInputStream(qrCode);
        //将二维码图片绘制在画布上,按照画布进行缩放
        qr2d.drawImage(ImageIO.read(bin), 0, 0, 192, 181, null);
        qr2d.dispose();
        //将二维码绘制到背景图上下文
        bill2d.drawImage(qrCodeImg, 70, 1110, 192, 181, null);
    }

    public static void drawZx(Graphics2D bill2d, ArticleDetailBean articleDetailBean, int y) throws IOException {
        BufferedImage image = ImageIO.read(new URL(articleDetailBean.getCoverImg()));
        bill2d.drawImage(image, 30, y, 690, 320, null);

        //颜色
        Color cc = new Color(17, 17, 17);
        // 设置画笔,设置Paint属性
        bill2d.setPaint(cc);
        //标题文字大小
        int titleSize = 36;
        //标题字体
        Font font = new Font("黑体", Font.BOLD, titleSize);
        bill2d.setFont(font);
        //文本宽度
        int txtWidth = 610;
        //标题
        String title = articleDetailBean.getTitle();

        //标题开始纵坐标
        y = 560;
        //内容开始横坐标
        int contentX = 70;
        //行间距
        int tileRowSize = 28;
        //标题最大行
        int maxTitleRow = 3;
        //一行可容纳标题字数
        int titleCount = (txtWidth / titleSize);

        y = drawString(bill2d, maxTitleRow, title, titleCount, contentX, y, tileRowSize, titleSize);
        //开始画具体内容
        y = y + 10;
        //内容
        String content = articleDetailBean.getContent();
        if (StringUtils.isEmpty(content)) {
            return;
        }
        //将富文本转为纯文本
        HtmlToText h = new HtmlToText();
        h.parse(content);
        List<String> sections = h.getString();
        //转成字符串
        content = String.join("", sections);
        //颜色
        Color color = new Color(17, 17, 17);
        // 设置画笔,设置Paint属性
        bill2d.setPaint(color);
        //标题文字大小
        int contentSize = 26;
        //行间距
        int contentRowSize = 28;
        //标题字体
        Font contentFont = new Font("黑体", Font.PLAIN, contentSize);
        bill2d.setFont(contentFont);

        //一行可容纳内容字数
        int contentCount = (txtWidth / contentSize);
        int maxContentRow = 4;
        drawString(bill2d, maxContentRow, content, contentCount, contentX, y, contentRowSize, contentSize);
    }

    /**
     * 需要多少行
     *
     * @param length   内容长度
     * @param rowCount 一行需要多少字
     * @param maxRow  最大多少行
     * @return
     */
    private static int getNeedRow(int length, int rowCount, int maxRow) {
        if ((maxRow * rowCount) < length) {
            return length / rowCount + 1;
        }
        return length / rowCount;
    }

    /**
     * 画文字
     *
     * @param bill2d
     * @param maxRow 画文字的最大行
     * @param content 文字内容
     * @param rowCount 一行允许多少字
     * @param contentX 横坐标开始位置
     * @param y 纵坐标开始位置
     * @param contentRowSize 文字行间距
     * @param contentSize 文字大小
     * @return
     */
    private static int drawString(Graphics2D bill2d, int maxRow, String content, int rowCount, int contentX, int y, int contentRowSize, int contentSize) {
        int startIndex = 0;
        int endIndex = rowCount;
        int needRow = getNeedRow(content.length(), rowCount, maxRow);
        for (int i = 1; i < maxRow + 1; i++) {
            String thisRowContent;
            if (i == maxRow && needRow > maxRow) {
                thisRowContent = content.substring(startIndex, endIndex - 2) + "...";
            } else {
                thisRowContent = content.substring(startIndex, endIndex);
            }
            bill2d.drawString(thisRowContent, contentX, y);
            startIndex = endIndex;
            endIndex = endIndex + rowCount;
            if (endIndex > content.length()) {
                endIndex = content.length();
            }
            y = y + contentRowSize + contentSize;
        }
        return y;
    }

}
